package xyz.migle.sc.demo;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/***
 * io.swagger.annotations.*相关的导入是用来演示如何生成在线api文档的.
 * 根据注解写入相关信息之后会自动生成在线的API文档，可以在 "/swagger-ui.html"路径下查看
 * 可以参考：https://www.xncoding.com/2017/06/09/restful/swagger.html 和 https://blog.xiaomo.info/2016/JavaSpringBootSwaggerUi/
 *  *  https://blog.csdn.net/xiaojin21cen/article/details/78654652
 */

@RestController
@Api(value = "DemoController001", description = "api样例")

public class DemoController001 {
    private static final Logger logger = LoggerFactory.getLogger(DemoController001.class);

    @Value("${iop.test}")
    private String tt;

    @Value("${spring.datasource.url}")
    private String url;

    private String timeout;

    @RequestMapping("/")
    public String home() {
        logger.info("i am sc-demo-app-001 {},{}" ,tt,url);
        return "i am sc-demo-app-001";
    }



    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/cal")
    //TODO 超时配置？？
    //https://www.cnblogs.com/lazio10000/p/6027581.html
    //https://blog.csdn.net/hry2015/article/details/78554846
    @HystrixCommand(fallbackMethod = "arithmeticError")  //无法正常提供服务时，比如超时，会调用fallbackMethod的方法，两个方法参数必须要一致

    @ApiOperation(value = "四则运算", notes = "两个整数的加、减、乘、除", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

    public String arithmetic(@RequestParam(name = "a", required = true) int a, @RequestParam(name = "b", required = true) int b) {

        //String msg = restTemplate.getForObject("http://sc-demo-app-002/",String.class);
        //System.out.println("msg:"+msg);

//     Map<String, Integer> uriVariables = new HashMap<>();
//     uriVariables.put("a", a);
//     uriVariables.put("b", b);
//??? why???
        logger.debug("a:{},b:{}", a, b);
        logger.info("a:{},b:{}", a, b);
        logger.warn("a:{},b:{}", a, b);
        logger.error("a:{},b:{}", a, b);
        int add = restTemplate.getForObject("http://sc-demo-app-002/add?a={a}&b={b}", Integer.class, a, b);
        int sub = restTemplate.getForObject("http://sc-demo-app-002/sub?a={a}&b={b}", Integer.class, a, b);
        int mult = restTemplate.getForObject("http://sc-demo-app-003/mult?a={a}&b={b}", Integer.class, a, b);
        double div = restTemplate.getForObject("http://sc-demo-app-003/div?a={a}&b={b}", Double.class, a, b);

        //return "add:" + sub;
        return "{add:" + add + ", sub:" + sub + ", mult:" + mult + ", div:" + div + "}";

    }

    //tips:参数要和正常访问的方法一致
    public String arithmeticError(int a,int b ){
        return String.format("let me thinking for a moment %d,%d", a,b);
    }
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
